﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class Hexagonsocketheadcapscrews
    {
        public static void _Hexagonsocketheadcapscrews(Dictionary<string, Object> data)//细柄丝锥
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            refin.Linetype = Linetype.Dashed;
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            centre.Linetype = Linetype.DashDot;
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);

            double scale = 5; //放大系数

            double d = Convert.ToDouble(data["d"]) * scale;
            double p = Convert.ToDouble(data["p"]) * scale;
            double dk = Convert.ToDouble(data["dk"]) * scale;
            double da = Convert.ToDouble(data["da"]) * scale;
            double ds = Convert.ToDouble(data["ds"]) * scale;
            double e = Convert.ToDouble(data["e"]) * scale;
            double k = Convert.ToDouble(data["k"]) * scale;
            double s = Convert.ToDouble(data["s"]) * scale;
            double t = Convert.ToDouble(data["t"]) * scale;
            double w = Convert.ToDouble(data["w"]) * scale;
            double l = Convert.ToDouble(data["l"]) * scale;
            double ls = Convert.ToDouble(data["ls"]) * scale;
            double lg = Convert.ToDouble(data["lg"]) * scale;


            //坐标
            Vector2 v1 = new Vector2(0, 0);
            Vector2 v2 = new Vector2(v1.X + l - 17, v1.Y);
            Vector2 v3 = new Vector2(v1.X + 4, v1.Y);
            Vector2 v4 = new Vector2(v3.X, v3.Y + dk / 2 - 0.5);
            Vector2 v5 = new Vector2(v4.X + 0.5, v4.Y + 0.5);
            Vector2 v6 = new Vector2(v5.X + k, v5.Y);
            Vector2 v7 = new Vector2(v6.X, v6.Y * -1);
            Vector2 v8 = new Vector2(v5.X, v5.Y * -1);
            Vector2 v9 = new Vector2(v4.X, v4.Y * -1);
            Vector2 v10 = new Vector2(v6.X, v3.Y + da / 2);
            Vector2 v11 = new Vector2(v10.X + 1, v3.Y + ds / 2);
            Vector2 v12 = new Vector2(v10.X + l - k - 40, v11.Y);
            Vector2 v13 = new Vector2(v12.X - 0.2, v3.Y + d / 6);
            Vector2 v14 = new Vector2(v13.X , v13.Y * -1);
            Vector2 v15 = new Vector2(v12.X, v12.Y * -1);
            Vector2 v16 = new Vector2(v11.X, v11.Y * -1);
            Vector2 v17 = new Vector2(v10.X, v10.Y * -1);
            Vector2 v18 = new Vector2(v12.X + 1, v12.Y);
            Vector2 v19 = new Vector2(v18.X + 10, v18.Y);
            Vector2 v20 = new Vector2(v19.X + 1, v19.Y - 1);
            Vector2 v21 = new Vector2(v20.X, v20.Y * -1);
            Vector2 v22 = new Vector2(v19.X, v19.Y * -1);
            Vector2 v23 = new Vector2(v15.X + 1, v15.Y);
            Vector2 v24 = new Vector2(v14.X + 1, v14.Y);
            Vector2 v25 = new Vector2(v13.X + 1, v13.Y);
            Vector2 v26 = new Vector2(v7.X + ls, v11.Y);
            Vector2 v27 = new Vector2(v7.X + lg, v26.Y - 0.5);
            Vector2 v28 = new Vector2(v12.X - 0.1, v27.Y);
            Vector2 v29 = new Vector2(v28.X + 1, v28.Y);
            Vector2 v30 = new Vector2(v19.X + 0.5, v19.Y - 0.5);
            Vector2 v31 = new Vector2(v26.X, v16.Y);
            Vector2 v32 = new Vector2(v27.X, v27.Y * -1);
            Vector2 v33 = new Vector2(v28.X, v28.Y * -1);
            Vector2 v34 = new Vector2(v33.X + 1, v33.Y);
            Vector2 v35 = new Vector2(v30.X, v30.Y * -1);
            Vector2 v44 = new Vector2(v3.X, v3.Y + e / 2);
            Vector2 v36 = new Vector2(v44.X + t, v44.Y);
            Vector2 v37 = new Vector2(v36.X + (e / 2) * Math.Tan(30 * pi / 180), v3.Y);
            Vector2 v38 = new Vector2(v36.X, v36.Y * -1);
            Vector2 v39 = new Vector2(v44.X, v44.Y * -1);
            Vector2 v40 = new Vector2(v3.X, v3.Y + e / 4);
            Vector2 v41 = new Vector2(v36.X, v40.Y);
            Vector2 v42 = new Vector2(v40.X, v40.Y * -1);
            Vector2 v43 = new Vector2(v41.X, v42.Y);

            Vector2 v45 = new Vector2(v2.X + dk * 2, v2.Y);
            Vector2 v46 = new Vector2(v45.X - dk / 2 - 4, v45.Y);
            Vector2 v47 = new Vector2(v45.X + dk / 2 + 4, v45.Y);
            Vector2 v48 = new Vector2(v45.X, v45.Y + dk / 2 + 4);
            Vector2 v49 = new Vector2(v45.X, v45.Y - dk / 2 - 4);
            Vector2 v50 = fun.Polar(v45, 30 * pi / 180, e / 2);
            Vector2 v51 = new Vector2(v50.X , v50.Y * -1);
            Vector2 v52 = new Vector2(v45.X, v45.Y - e / 2);
            Vector2 v53 = fun.Polar(v45, -150 * pi / 180, e / 2);
            Vector2 v54 = new Vector2(v53.X, v53.Y * -1);
            Vector2 v55 = new Vector2(v45.X, v45.Y + e / 2);




            //作图
            Line line1 = new Line(v1, v2);
            line1.Layer = centre;
            line1.Linetype = Linetype.DashDot;
            line1.LinetypeScale = 3;
            dxf.AddEntity(line1);

            Line line2 = new Line(v44, v36);
            line2.Layer = refin;
            line2.Linetype = Linetype.Dashed;
            line2.LinetypeScale = 5;
            dxf.AddEntity(line2);

            Line line3 = new Line(v36, v37);
            line3.Layer = refin;
            line3.Linetype = Linetype.Dashed;
            line3.LinetypeScale = 5;
            dxf.AddEntity(line3);

            Line line4 = new Line(v38, v37);
            line4.Layer = refin;
            line4.Linetype = Linetype.Dashed;
            line4.LinetypeScale = 5;
            dxf.AddEntity(line4);

            Line line5 = new Line(v38, v39);
            line5.Layer = refin;
            line5.Linetype = Linetype.Dashed;
            line5.LinetypeScale = 0.2;
            dxf.AddEntity(line5);

            Line line6 = new Line(v42, v43);
            line6.Layer = refin;
            line6.Linetype = Linetype.Dashed;
            line6.LinetypeScale = 0.2;
            dxf.AddEntity(line6);

            Line line7 = new Line(v40, v41);
            line7.Layer = refin;
            line7.Linetype = Linetype.Dashed;
            line7.LinetypeScale = 0.2;
            dxf.AddEntity(line7);

            LwPolyline line11 = new LwPolyline();
            line11.Vertexes.Add(new LwPolylineVertex(v36));
            line11.Vertexes.Add(new LwPolylineVertex(v41));
            line11.Vertexes.Add(new LwPolylineVertex(v43));
            line11.Vertexes.Add(new LwPolylineVertex(v38));
            line11.Vertexes[0].Bulge = Math.Tan(-55 * pi / 180 / 4);
            line11.Vertexes[1].Bulge = Math.Tan(-48 * pi / 180 / 4);
            line11.Vertexes[2].Bulge = Math.Tan(-55 * pi / 180 / 4);
            fun.PolyFill(dxf, line11, 0.1, refin);

            LwPolyline line12 = new LwPolyline();
            line12.Vertexes.Add(new LwPolylineVertex(v4));
            line12.Vertexes.Add(new LwPolylineVertex(v5));
            line12.Vertexes.Add(new LwPolylineVertex(v6));
            line12.Vertexes.Add(new LwPolylineVertex(v7));
            line12.Vertexes.Add(new LwPolylineVertex(v8));
            line12.Vertexes.Add(new LwPolylineVertex(v9));
            line12.Vertexes.Add(new LwPolylineVertex(v4));
            line12.Vertexes[0].Bulge = Math.Tan(-90 * pi / 180 / 4);
            line12.Vertexes[4].Bulge = Math.Tan(-90 * pi / 180 / 4);
            fun.PolyFill(dxf, line12, 0.3, contour);

            LwPolyline line13 = new LwPolyline();
            line13.Vertexes.Add(new LwPolylineVertex(v10));
            line13.Vertexes.Add(new LwPolylineVertex(v11));
            line13.Vertexes.Add(new LwPolylineVertex(v12));
            line13.Vertexes.Add(new LwPolylineVertex(v28));
            line13.Vertexes.Add(new LwPolylineVertex(v13));
            line13.Vertexes.Add(new LwPolylineVertex(v14));
            line13.Vertexes.Add(new LwPolylineVertex(v33));
            line13.Vertexes.Add(new LwPolylineVertex(v15));
            line13.Vertexes.Add(new LwPolylineVertex(v16));
            line13.Vertexes.Add(new LwPolylineVertex(v17));
            line13.Vertexes[0].Bulge = Math.Tan(82 * pi / 180 / 4);
            line13.Vertexes[2].Bulge = Math.Tan(2 * pi / 180 / 4);
            line13.Vertexes[3].Bulge = Math.Tan(11 * pi / 180 / 4);
            line13.Vertexes[4].Bulge = Math.Tan(-2 * pi / 180 / 4);
            line13.Vertexes[5].Bulge = Math.Tan(11 * pi / 180 / 4);
            line13.Vertexes[6].Bulge = Math.Tan(2 * pi / 180 / 4);
            line13.Vertexes[8].Bulge = Math.Tan(82 * pi / 180 / 4);
            fun.PolyFill(dxf, line13, 0.3, contour);

            LwPolyline line14 = new LwPolyline();
            line14.Vertexes.Add(new LwPolylineVertex(v18));
            line14.Vertexes.Add(new LwPolylineVertex(v29));
            line14.Vertexes.Add(new LwPolylineVertex(v25));
            line14.Vertexes.Add(new LwPolylineVertex(v24));
            line14.Vertexes.Add(new LwPolylineVertex(v34));
            line14.Vertexes.Add(new LwPolylineVertex(v23));
            line14.Vertexes.Add(new LwPolylineVertex(v22));
            line14.Vertexes.Add(new LwPolylineVertex(v21));
            line14.Vertexes.Add(new LwPolylineVertex(v20));
            line14.Vertexes.Add(new LwPolylineVertex(v19));
            line14.Vertexes.Add(new LwPolylineVertex(v18));
            line14.Vertexes[0].Bulge = Math.Tan(2 * pi / 180 / 4);
            line14.Vertexes[1].Bulge = Math.Tan(11 * pi / 180 / 4);
            line14.Vertexes[2].Bulge = Math.Tan(-2 * pi / 180 / 4);
            line14.Vertexes[3].Bulge = Math.Tan(11 * pi / 180 / 4);
            line14.Vertexes[4].Bulge = Math.Tan(2 * pi / 180 / 4);
            fun.PolyFill(dxf, line14, 0.3, contour);

            LwPolyline line15 = new LwPolyline();
            line15.Vertexes.Add(new LwPolylineVertex(v26));
            line15.Vertexes.Add(new LwPolylineVertex(v27));
            line15.Vertexes.Add(new LwPolylineVertex(v28));
            fun.PolyFill(dxf, line15, 0.15, contour);

            LwPolyline line16 = new LwPolyline();
            line16.Vertexes.Add(new LwPolylineVertex(v31));
            line16.Vertexes.Add(new LwPolylineVertex(v32));
            line16.Vertexes.Add(new LwPolylineVertex(v33));
            fun.PolyFill(dxf, line16, 0.15, contour);

            LwPolyline line17 = new LwPolyline();
            line17.Vertexes.Add(new LwPolylineVertex(v27));
            line17.Vertexes.Add(new LwPolylineVertex(v32));
            fun.PolyFill(dxf, line17, 0.3, contour);

            LwPolyline line18 = new LwPolyline();
            line18.Vertexes.Add(new LwPolylineVertex(v19));
            line18.Vertexes.Add(new LwPolylineVertex(v22));
            fun.PolyFill(dxf, line18, 0.3, contour);

            LwPolyline line19 = new LwPolyline();
            line19.Vertexes.Add(new LwPolylineVertex(v29));
            line19.Vertexes.Add(new LwPolylineVertex(v30));
            fun.PolyFill(dxf, line19, 0.15, contour);

            LwPolyline line20 = new LwPolyline();
            line20.Vertexes.Add(new LwPolylineVertex(v34));
            line20.Vertexes.Add(new LwPolylineVertex(v35));
            fun.PolyFill(dxf, line20, 0.15, contour);

            Line line21 = new Line(v46, v47);
            line21.Layer = centre;
            line21.Linetype = Linetype.DashDot;
            line21.LinetypeScale = 3;
            dxf.AddEntity(line21);

            Line line22 = new Line(v48, v49);
            line22.Layer = centre;
            line22.Linetype = Linetype.DashDot;
            line22.LinetypeScale = 3;
            dxf.AddEntity(line22);

            fun.drawcircle(dxf, v45, e / 2, 0.3, contour);

            fun.drawcircle(dxf, v45, dk / 2, 0.3, contour);

            LwPolyline line23 = new LwPolyline();
            line23.Vertexes.Add(new LwPolylineVertex(v50));
            line23.Vertexes.Add(new LwPolylineVertex(v51));
            line23.Vertexes.Add(new LwPolylineVertex(v52));
            line23.Vertexes.Add(new LwPolylineVertex(v53));
            line23.Vertexes.Add(new LwPolylineVertex(v54));
            line23.Vertexes.Add(new LwPolylineVertex(v55));
            line23.Vertexes.Add(new LwPolylineVertex(v50));
            fun.PolyFill(dxf, line23, 0.3, contour);



            //标注
            double height = 2;
            double arrowsize = 1;
            double exline = 2;


            fun.dimrot1(dxf, v44, v36, 0, dk, 1, height, arrowsize, exline, "t=" + t, "", "");

            fun.dimrot1(dxf, v6, v36, 0, dk / 2, 1, height, arrowsize, exline, "w=" + w, "", "");

            fun.dimrot1(dxf, v5, v8, 90, dk / 2, 1, height, arrowsize, exline, "dk=" + dk, "", "");

            fun.dimrot1(dxf, v9, v7, 180, dk / 2, 1, height, arrowsize, exline, "k=" + k, "", "");

            fun.dimrot1(dxf, v7, v31, 180, dk / 2, 1, height, arrowsize, exline, "ls=" + ls, "", "");

            fun.dimrot1(dxf, v7, v32, 180, dk, 1, height, arrowsize, exline, "lg=" + lg, "", "");

            fun.dimrot1(dxf, v7, v21, 180, dk * 3 / 2, 1, height, arrowsize, exline, "l=" + l, "", "");

            fun.dimrot1(dxf, v19, v22, -90, dk / 2, 1, height, arrowsize, exline, "d=" + d , "", "");

            fun.dimrot1(dxf, v10, v17, -90, dk / 2, 1, height, arrowsize, exline, "da=" + da, "", "");

            fun.dimrot1(dxf, v10, v16, -90, dk, 1, height, arrowsize, exline, "ds=" + ds, "", "");

            fun.dimrot1(dxf, v55, v52, -90, dk, 1, height, arrowsize, exline, "e=" + e, "", "");

            fun.dimrot1(dxf, v53, v51, 180, dk, 1, height, arrowsize, exline, "s=" + s, "", "");


            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }





            //定义格式，路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);


        }

    }
}